// 请求通用方法
import apiMixin from '@/mixin/apiMixin';
// 任务通用方法
import taskMixin from '@/mixin/taskMixin';

import fetch from '@/api/axios';

import utils from '@/utils/index';
import envConfig from '@/config/env.js';

import addIco from '../../../../static/img/addImg2.png';

export default {
    mixins: [apiMixin,taskMixin],
    data() {
        return {
            loading: false,
            projectList: {
                name: '',
                accept_tip: '',
                publish_tip: '',
                min_total_accept: '',
                min_amount: '',
                max_total_step: '',
                max_files: '',
                sort: '',
                template_id: '',
                template: [],
                min_user_level: '',
                status: false,
                is_allow_custom_title: false,
                icon: '',
                timing_type: '',
                timing_period: '',
                timing_max_accepts: '',
                alias_id:0,
                auto_pass:false,
            },
            alias:[],
            authorized_key:  utils.handleLocalStorage.get('authorized_key'),
            host: envConfig.baseUrl,
            bid: 0,
        }
    },
    mounted() {
        this.init();
    },
    methods: {
        createBid() {
            let prefix = utils.getUploadPrefix();
            let bid = prefix + '-' +  utils.createBidFn();
            return  bid;
        },
        init() {
            this.bid = this.createBid();
            this.getProjectDetailByIdApi();
            this.getAlias();
        },
        getAlias(){
          var _this = this;
          fetch.get('/api/console/task/project/alias').then((res)=>{
          	let data = res.data;
          	if(data.code == 0){
              _this.alias = res.data.data;
          	}else{
          		_this.$message.error(data.message);
          	}
          })
        },
        getProjectDetailByIdApi() {
            let id = this.$route.query.id;
            let _self = this;
            this.comResquest(this.request.getProjectDetailById,(res)=>{
                let { result } = res;
                _self.projectList = result;
                if (result.min_amount) {
                    result.min_amount = _self.priceFormat(result.min_amount);
                }
            },{
                id,
                load: true
            });
        },
        handleUploadSuccess(res) {
            let { code,data } = res;
            if (Number(code) === 0) {
                this.projectList.icon = data.url;
            }
        },
        posetEditProjectDetailApi() {
            let id = this.$route.query.id;
            let subjectId = this.$route.query.subjectId;
            let _self = this;
            let update_data = utils.simpleCopy(this.projectList);
            if (id) {
                update_data.id = id;
            }else{
                update_data.platform_id = subjectId;
            }
            update_data.bid = this.bid;
            update_data.min_amount = update_data.min_amount * envConfig.PRICE_RADIUS;
            delete update_data.template;
            delete update_data.help;
            delete update_data.time_created;
            this.comResquest(this.request.posetEditProjectDetail,(res)=>{
                _self.$message.success("保存成功！");
            },{
                update_data,
                load: true
            });
        },
        onSubmit() {
			this.posetEditProjectDetailApi();
        },
        uploadImgShow(icon) {
            return icon || addIco;
        }
    },
}
